package cn.stylefeng.guns.modular.controller;


import java.time.LocalDateTime;
import java.util.Date;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.entity.IdjBanner;
import cn.stylefeng.guns.modular.entity.IdjImages;
import cn.stylefeng.guns.modular.service.IIdjBannerService;
import cn.stylefeng.guns.modular.service.IIdjImagesService;
import cn.stylefeng.guns.modular.util.JsonResult;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author TANGWJ
 * @since 2020-08-08
 */
@Controller
@RequestMapping("/modular/images")
public class IdjImagesController {


	private static String PREFIX = "/images/";

	private static final int SHORT_MAX = 65536;

	private static int counter = -1;

	@Autowired
	private IIdjImagesService service;

	/**
	 * 跳转到用户的首页
	 */
	@RequestMapping
    public ModelAndView index() {
        ModelAndView index = service.getList(1);
        index.setViewName(PREFIX + "images.html");
        return index;
    }
	
	@RequestMapping("/show")
    public ModelAndView show() {
        ModelAndView index = service.getList(2);
        index.setViewName(PREFIX + "images_list.html");
        return index;
    }
	
	@RequestMapping("/update")
	@ResponseBody
	public ResponseData update(@RequestBody IdjImages images) {
		if (ToolUtil.isEmpty(images.getId())) {
			throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
		}
		images.setUpdateTime(new Date());
		service.setStatus(images);
		return ResponseData.success();
	}

	@RequestMapping("/insert")
	@ResponseBody
	public ResponseData insert(@RequestBody IdjImages images) {
		images.setId(nextId());
		images.setCreateTime(new Date());
		images.setUpdateTime(new Date());
		service.insert(images);
		return ResponseData.success();
	}

	public static synchronized long nextId() {
		long now = System.currentTimeMillis();
		if (counter == -1) {
			long seed = now ^ Thread.currentThread().getId();
			Random rnd = new Random(Long.hashCode(seed));
			counter = rnd.nextInt(SHORT_MAX);
		}
		long id = (now << 16) | counter;
		counter = (counter + 1) % SHORT_MAX;
		return id;
	}
}

